import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'


/**
 * 静态路由 - 只放所有人都能访问的路由
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },


  {
    path: '/import',
    component: Layout,
    // 如果不加可能会出现在左侧导航栏
    hidden: true,
    children: [
      { 
        path: '',
        name: 'import',
        component: () => import('@/views/import/index')
      }
    ]
  },

  {
    path: '/employees/detail/:id',
    component: Layout,
    hidden: true,
    children: [
      { 
        path: '',
        name: 'EmpDetail',
        component:  () => import('@/views/employees/EmpDetail'),
        meta: {
          // 还是要给一个title，埋个伏笔后面要用
          title: '员工详情'
        }
      }
    ]
  },

  {
    path: '/print',
    component: Layout,
    hidden: true,
    children: [
      { 
        path: '',
        name: 'print',
        component:  () => import('@/views/employees/print'),
      }
    ]
  },
]

// 导入动态的路由
import departments from './modules/departments.js'
import employees from './modules/employees.js'
import setting from './modules/setting.js'
import salarys from './modules/salarys.js'
import social from './modules/social.js'
import attendances from './modules/attendances.js'
import approvals from './modules/approvals.js'
import permission from './modules/permission.js'

// 这是动态路由的数组 -- 目前这里暂时写死，以后根据每个账号的权限，会让这个数组有条件变化
// 把动态路由暴露出来，是因为在别的地方拿到menus数据后要做过滤
export const asyncRoutes = [
  departments,
  employees,
  setting,
  salarys,
  social,
  attendances,
  approvals,
  permission
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    // 路由表,初始化的时候写死，它新版本内部有优化，会自动把404提到最后
    // routes: [...constantRoutes, ...asyncRoutes]
    // 少了动态路由的那部分
    routes: [...constantRoutes]
  })

const router = createRouter()

// 专门用来重置路由的
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
